package test;

import java.io.IOException;
//import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
//import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class DWExportNode {

	private XMLEventWriter eventWriter;
	private XMLEventFactory eventFactory;
	private DWObject obj;
	private int indention;
	
	List <Attribute> attributeList = new ArrayList<Attribute>();//Arrays.asList();
	List <Object>nsList  = Arrays.asList();
	 
	
	XMLEvent end;
	XMLEvent tab;
	
	public DWExportNode(XMLEventFactory pEventFactory, XMLEventWriter pEventWriter, DWObject pObj) {
		this(pEventFactory,pEventWriter, pObj, 1);	
	}

	public DWExportNode(XMLEventFactory pEventFactory, XMLEventWriter pEventWriter, DWObject pObj, int pIndention) {
		eventFactory=pEventFactory;
		eventWriter=pEventWriter;
		obj = pObj;
		indention=pIndention;
	}
	
	public void export( ) throws XMLStreamException, IOException  {
		Iterator <DWObject>dwoIterator;
		createNode();
		setAttributes();
		createStartNode();
		
		if (obj.getNodeValue()==null) {
			System.out.println("no value for <"+obj.getNodeName()+">");
			dwoIterator=obj.getChildrenIterator();
			while(dwoIterator.hasNext()){
				//dwoIterator.next();
				new DWExportNode(eventFactory ,eventWriter, dwoIterator.next(),indention+1).export();
			}
		} else {
			createContent();
		}
		createEndNode();
	}
	
	private void createNode() throws XMLStreamException {
		
		//eventFactory = XMLEventFactory.newInstance();
		end = eventFactory.createDTD("\n");
		tab = eventFactory.createDTD("\t");
	}
	private void setAttributes() throws XMLStreamException {
		//obj.getAttributes().entr
		//Map smth=new HashMap();
		for (Map.Entry<String, String> entry : obj.getAttributes().entrySet() ){
			//Map.Entry<String, String> entry : map.entrySet()
			System.out.println(entry.getKey() + "/" + entry.getValue());
			// Create Attribs
			Attribute attribute = eventFactory.createAttribute(obj.getPrefix(entry.getKey()),"",entry.getKey(), entry.getValue());
			
			System.out.println("PREFIX ="+ obj.getPrefix("lang") );
			System.out.println("ENTRY ="+entry.getKey());
			
			//attributeList = Arrays.asList(attribute);
			attributeList.add(attribute);
		    /*try {
		    	//nsList = Arrays.asList(obj.getNameSpace(entry.getKey())+"" );
		    	//nsList = Arrays.asList(eventFactory.createNamespace("xmll"));
		    	nsList = Arrays.asList();
		    } catch (Exception e) {
		    	nsList = Arrays.asList();
		    	System.out.println("DWExportNode-setAttributes Exception:"+e);
		    }*/
		    //Arrays.as
			
			}
	}
	private void createStartNode() throws XMLStreamException {										// Create Start node
		String oName = obj.getNodeName();
		Iterator<Attribute> aIt=attributeList.iterator();
		Iterator<Object> nIt=nsList.iterator();
		
		StartElement sElement = eventFactory.createStartElement("", "", oName, aIt, nIt );
		//if(indention==1) 
		eventWriter.add(end);
		for (int i=0;i<indention;i++) eventWriter.add(tab);
		eventWriter.add(sElement);
	}
	
	private void createEndNode() throws XMLStreamException {										// Create End node
		//eventWriter.add(tab);
		EndElement eElement = eventFactory.createEndElement("", "", obj.getNodeName());
		if(indention==1) {
			eventWriter.add(end);
			eventWriter.add(tab);
		}

		eventWriter.add(eElement);
		//eventWriter.add(end);
	}
	private void createContent() throws XMLStreamException, IOException {										// Create Content
		
		Characters characters = eventFactory.createCharacters(obj.getNodeValue());
		/*
		Characters characters = eventFactory.createCharacters(
									new String (	obj.getNodeValue().getBytes(),
													"UTF-8"
											)  
				);
		*/
		//eventWriter.add(eventFactory.createCData(obj.getNodeValue()) );
		eventWriter.add(characters);
	}
	
	
}
